import Vue from 'vue'
import VueRouter from 'vue-router'
// import Home from '../views/Home.vue'
import { getUserByTokenApi } from "../request/api"
import store from '../store'

Vue.use(VueRouter)

const routes = [
  {
    path: "/login",
    name: "Login",
    component: () => import(/* webpackChunkName: "login" */"../views/login/Login.vue")
  }
]

const dynamicRoutes = {
  "/good": {
    path: "/good",
    name: "Good",
    component: () => import(/* webpackChunkName: "good" */"../views/good/Good.vue")
  },
  "/attribute": {
    path: "/attribute",
    name: "Attribute",
    component: () => import(/* webpackChunkName: "attribute" */"../views/attribute/Attribute.vue")
  },
  "/brand": {
    path: "/brand",
    name: "Brand",
    component: () => import(/* webpackChunkName: "brand" */"../views/brand/Brand.vue")
  },
  "/category": {
    path: "/category",
    name: "Category",
    component: () => import(/* webpackChunkName: "category" */"../views/category/Category.vue")
  },
  "/order": {
    path: "/order",
    name: "Order",
    component: () => import(/* webpackChunkName: "order" */"../views/order/Order.vue")
  },
  "/statdata": {
    path: "/statdata",
    name: "Statdata",
    component: () => import(/* webpackChunkName: "statdata" */"../views/statdata/Statdata.vue")
  },
  "/topic": {
    path: "/topic",
    name: "Topic",
    component: () => import(/* webpackChunkName: "topic" */"../views/topic/Topic.vue")
  },
  "/addgood": {
    path: "/addgood",
    name: "Addgood",
    component: () => import(/* webpackChunkName: "addgood" */"../views/addgood/Addgood.vue")
  },
  "/addcategory": {
    path: "/addcategory",
    name: "Addcategory",
    component: () => import(/* webpackChunkName: "addgood" */"../views/addcategory/Addcategory.vue")
  }
}

const router = new VueRouter({
  routes
})

const addDynamicRoutes = () => {
  // 动态添加路由配置
  let newRoutes = []; //新的路由数组
  for (let i = 0; i < store.state.menu.length; i++) {
    let path = store.state.menu[i].path;
    if (dynamicRoutes[path]) {
      newRoutes.push(dynamicRoutes[path]);
    }
  }
  console.log(newRoutes);
  // 添加路由配置
  router.addRoutes([
    {
      path: "/homepage",
      name: "Homepage",
      component: () => import(/* webpackChunkName: "homepage" */"../views/homepage/Homepage.vue"),
      children: newRoutes
    },
    {
      path: "*",
      redirect: "/homepage"
    }
  ])
}
router.beforeEach((to, from, next) => {
  // console.log(from, to)
  let token = localStorage.getItem("token")
  //  不是登录页面跳转过来 homepage
  // if (to.path != "/login" && token && !(to.path == '/homepage' && from.path == '/login'))
  if (to.path != "/login" && token && store.state.menu.length == 0) {
    // 发送请求
    getUserByTokenApi().then(res => {
      if (res.errno == 0) {
        // 用户权限列表存到vuex里面去
        store.commit("SAVEMENU", res.data.menu);
        // 动态添加路由配置
        addDynamicRoutes()
        next(to);
      } else {
        // 失败
        localStorage.clear();
        next('/login')
      }
    })
  } else if (to.path == '/homepage' && from.path == '/login' && store.state.menu.length > 0) {
    //动态添加路由配置
    addDynamicRoutes();
    next(store.state.menu[0].path)
  } else if (!token && to.path != '/login') {
    next('/login')
  } else if (to.path == "/login" && token) {
    next(from)
  } else {
    next()
  }
})
export default router
